home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / PatchTraps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-13  |  8.5 KB  |  292 lines  |  [TEXT/CWIE]

  1. /*
  2. PatchTraps.c
  3.  
  4.     Patch various traps associated with events. The argument is a timeout, specifying how long to disable the trap.
  5. Supplying a value of zero re-enables the trap immediately. Each trap is disabled separately, but if timeout occurs
  6. all traps are re-enabled. The idea is that in normal use the timeout is a safety. Normally you'll disable the trap
  7. do something for a while, and then re-enable it. If disaster strikes, and your program is interrupted by an
  8. error before reenabling the traps then you might be stuck with a dead keyboard
  9. and mouse. Fortunately all you have to do is wait for the timeout and then everything will come back to life,
  10. sparing you the necessity of rebooting.
  11.  
  12. My testing of these routines has been very limited. The Event-related patches seem to work ok,
  13. as does the GetMenuBar patch. The Munger trap patch crashes, but that's probably because of 
  14. the printf statements.
  15.  
  16. HISTORY:
  17. 4/7/97    dgp wrote it, based on AtExitToShell.c
  18. 4/8/97    dgp added time out
  19. */
  20. #ifndef _VIDEOTOOLBOX_
  21.     #include "VideoToolbox.h"
  22. #endif
  23. #ifndef __TRAPS__
  24.     #include <Traps.h>
  25. #endif
  26. #include <string.h>
  27. //<Events.h>
  28. //<Windows.h>
  29. //<OSUtils.h>
  30. pascal Boolean MyEventAvail(EventMask eventMask, EventRecord *theEvent);
  31. pascal Boolean MyGetNextEvent(EventMask eventMask, EventRecord *theEvent);
  32. pascal Boolean MyWaitNextEvent(EventMask eventMask, EventRecord *theEvent, UInt32 sleep, RgnHandle mouseRgn);
  33. pascal void MyBeginUpdate(WindowRef theWindow);
  34. pascal void MyEndUpdate(WindowRef theWindow);
  35. pascal void MyPrimeTime(QElemPtr tmTaskPtr,long count);
  36. pascal Handle MyGetMenuBar(void);
  37. pascal long MyMunger(Handle h,long offset,const void *ptr1, long len1,const void *ptr2,long len2);
  38. void PatchEventAvail(long ticks);
  39. void PatchGetNextEvent(long ticks);
  40. void PatchWaitNextEvent(long ticks);
  41. void PatchBeginUpdate(long ticks);
  42. void PatchEndUpdate(long ticks);
  43. void PatchPrimeTime(long ticks);
  44. void PatchGetMenuBar(long ticks);
  45. void PatchMunger(long ticks);
  46. static void RestoreEvents(void);
  47. static long timeout;
  48. #include <LowMem.h>
  49.  
  50. void PatchEventAvail(long ticks)
  51. {
  52.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  53.  
  54.     if(ticks>0){
  55.         timeout=ticks+LMGetTicks();
  56.         if(oldTrapAddress==NULL){
  57.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_EventAvail);
  58.             newUPP=NewRoutineDescriptor((ProcPtr)MyEventAvail,kPascalStackBased,GetCurrentISA());
  59.             AtExitToShell(RestoreEvents);
  60.         }
  61.         SetToolTrapAddress((UniversalProcPtr)newUPP,_EventAvail);
  62.     }else{
  63.         if(oldTrapAddress!=NULL)
  64.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_EventAvail);
  65.     }
  66. }
  67.  
  68. void PatchGetNextEvent(long ticks)
  69. {
  70.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  71.  
  72.     if(ticks>0){
  73.         timeout=ticks+LMGetTicks();
  74.         if(oldTrapAddress==NULL){
  75.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_GetNextEvent);
  76.             newUPP=NewRoutineDescriptor((ProcPtr)MyGetNextEvent,kPascalStackBased,GetCurrentISA());
  77.             AtExitToShell(RestoreEvents);
  78.         }
  79.         SetToolTrapAddress((UniversalProcPtr)newUPP,_GetNextEvent);
  80.     }else{
  81.         if(oldTrapAddress!=NULL)
  82.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_GetNextEvent);
  83.     }
  84. }
  85.  
  86. void PatchWaitNextEvent(long ticks)
  87. {
  88.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  89.  
  90.     if(ticks>0){
  91.         timeout=ticks+LMGetTicks();
  92.         if(oldTrapAddress==NULL){
  93.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_WaitNextEvent);
  94.             newUPP=NewRoutineDescriptor((ProcPtr)MyWaitNextEvent,kPascalStackBased,GetCurrentISA());
  95.             AtExitToShell(RestoreEvents);
  96.         }
  97.         SetToolTrapAddress((UniversalProcPtr)newUPP,_WaitNextEvent);
  98.     }else{
  99.         if(oldTrapAddress!=NULL)
  100.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_WaitNextEvent);
  101.     }
  102. }
  103.  
  104. void PatchBeginUpdate(long ticks)
  105. {
  106.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  107.  
  108.     if(ticks>0){
  109.         timeout=ticks+LMGetTicks();
  110.         if(oldTrapAddress==NULL){
  111.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_BeginUpDate);
  112.             newUPP=NewRoutineDescriptor((ProcPtr)MyBeginUpdate,kPascalStackBased,GetCurrentISA());
  113.             AtExitToShell(RestoreEvents);
  114.         }
  115.         SetToolTrapAddress((UniversalProcPtr)newUPP,_BeginUpDate);
  116.     }else{
  117.         if(oldTrapAddress!=NULL)
  118.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_BeginUpDate);
  119.     }
  120. }
  121.  
  122. void PatchEndUpdate(long ticks)
  123. {
  124.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  125.  
  126.     if(ticks>0){
  127.         timeout=ticks+LMGetTicks();
  128.         if(oldTrapAddress==NULL){
  129.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_EndUpDate);
  130.             newUPP=NewRoutineDescriptor((ProcPtr)MyEndUpdate,kPascalStackBased,GetCurrentISA());
  131.             AtExitToShell(RestoreEvents);
  132.         }
  133.         SetToolTrapAddress((UniversalProcPtr)newUPP,_EndUpDate);
  134.     }else{
  135.         if(oldTrapAddress!=NULL)
  136.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_EndUpDate);
  137.     }
  138. }
  139.  
  140. void PatchPrimeTime(long ticks)
  141. {
  142.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  143.  
  144.     if(ticks>0){
  145.         timeout=ticks+LMGetTicks();
  146.         if(oldTrapAddress==NULL){
  147.             oldTrapAddress=(UniversalProcPtr)GetOSTrapAddress(_PrimeTime);
  148.             newUPP=NewRoutineDescriptor((ProcPtr)MyPrimeTime,kPascalStackBased,GetCurrentISA());
  149.             AtExitToShell(RestoreEvents);
  150.         }
  151.         SetOSTrapAddress((UniversalProcPtr)newUPP,_PrimeTime);
  152.     }else{
  153.         if(oldTrapAddress!=NULL)
  154.             SetOSTrapAddress((UniversalProcPtr)oldTrapAddress,_PrimeTime);
  155.     }
  156. }
  157.  
  158. void PatchGetMenuBar(long ticks)
  159. {
  160.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  161.  
  162.     if(ticks>0){
  163.         timeout=ticks+LMGetTicks();
  164.         if(oldTrapAddress==NULL){
  165.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_GetMenuBar);
  166.             newUPP=NewRoutineDescriptor((ProcPtr)MyGetMenuBar,kPascalStackBased,GetCurrentISA());
  167.             AtExitToShell(RestoreEvents);
  168.         }
  169.         SetToolTrapAddress((UniversalProcPtr)newUPP,_GetMenuBar);
  170.     }else{
  171.         if(oldTrapAddress!=NULL)
  172.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_GetMenuBar);
  173.     }
  174. }
  175.  
  176. static UniversalProcPtr oldMunger=NULL;
  177.  
  178. void PatchMunger(long ticks)
  179. {
  180.     static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
  181.  
  182.     if(ticks>0){
  183.         timeout=ticks+LMGetTicks();
  184.         if(oldTrapAddress==NULL){
  185.             oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_Munger);
  186.             oldMunger=oldTrapAddress;
  187.             newUPP=NewRoutineDescriptor((ProcPtr)MyMunger,kPascalStackBased,GetCurrentISA());
  188.             AtExitToShell(RestoreEvents);
  189.         }
  190.         SetToolTrapAddress((UniversalProcPtr)newUPP,_Munger);
  191.     }else{
  192.         if(oldTrapAddress!=NULL)
  193.             SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_Munger);
  194.     }
  195. }
  196.  
  197. static void RestoreEvents(void)
  198. {
  199.     PatchEventAvail(0);
  200.     PatchGetNextEvent(0);
  201.     PatchWaitNextEvent(0);
  202.     PatchBeginUpdate(0);
  203.     PatchEndUpdate(0);
  204.     PatchPrimeTime(0);
  205.     PatchMunger(0);
  206. }
  207.  
  208. pascal Boolean MyEventAvail(EventMask eventMask, EventRecord *theEvent)
  209. {
  210.     eventMask;theEvent;
  211.     //printf("MyEventAvail\n");
  212.     if(LMGetTicks()>=timeout)RestoreEvents();
  213.     return 0;
  214. }
  215.  
  216. pascal Boolean MyGetNextEvent(EventMask eventMask, EventRecord *theEvent)
  217. {
  218.     eventMask;theEvent;
  219.     //printf("MyGetNextEvent\n");
  220.     if(LMGetTicks()>=timeout)RestoreEvents();
  221.     return 0;
  222. }
  223.  
  224. pascal Boolean MyWaitNextEvent(EventMask eventMask, EventRecord *theEvent, UInt32 sleep, RgnHandle mouseRgn)
  225. {
  226.     eventMask;theEvent;sleep;mouseRgn;
  227.     //printf("MyWaitNextEvent\n");
  228.     if(LMGetTicks()>=timeout)RestoreEvents();
  229.     return 0;
  230. }
  231.  
  232. pascal void MyBeginUpdate(WindowRef theWindow)
  233. {
  234.     theWindow;
  235.     //printf("MyBeginUpdate\n");
  236.     if(LMGetTicks()>=timeout)RestoreEvents();
  237. }
  238.  
  239. pascal void MyEndUpdate(WindowRef theWindow)
  240. {
  241.     theWindow;
  242.     //printf("MyEndUpdate\n");
  243.     if(LMGetTicks()>=timeout)RestoreEvents();
  244. }
  245.  
  246. pascal void MyPrimeTime(QElemPtr tmTaskPtr,long count)
  247. {
  248.     tmTaskPtr;count;
  249.     //printf("MyPrimeTime\n");
  250.     if(LMGetTicks()>=timeout)RestoreEvents();
  251. }
  252.  
  253. pascal Handle MyGetMenuBar(void)
  254. {
  255.     //printf("MyGetMenuBar\n");
  256.     if(LMGetTicks()>=timeout)RestoreEvents();
  257.     return NULL;
  258. }
  259.  
  260. pascal long MyMunger(Handle h,long offset,const void *ptr1, long len1,const void *ptr2,long len2)
  261. {
  262.     char string[256];
  263.     long size,result;
  264.  
  265.     PatchMunger(0);    // printf calls Munger. So remove patch before using printf.
  266.     printf("MyMunger\n");
  267.     printf("h=0x%lx, (size h)=%ld, offset=%ld, ptr1=0x%lx, len1=%ld, ptr2=0x%lx, len2=%ld\n"
  268.         ,h,GetHandleSize(h),offset,ptr1,len1,ptr2,len2);
  269.     string[255]=0;
  270.     size=GetHandleSize(h);
  271.     if(size>sizeof(string)-1)size=sizeof(string)-1;
  272.     strncpy(string,*h+offset,size);
  273.     string[size]=0;
  274.     printf("h=%s\n",string);
  275.     strncpy(string,ptr1,len1);
  276.     string[len1]=0;
  277.     printf("ptr1=%s\n",string);
  278.     strncpy(string,ptr2,len2);
  279.     string[len2]=0;
  280.     printf("ptr2=%s\n",string);
  281.  
  282.     result=Munger(h,offset,ptr1,len1,ptr2,len2);
  283.  
  284.     size=GetHandleSize(h);
  285.     if(size>sizeof(string)-1)size=sizeof(string)-1;
  286.     strncpy(string,*h+offset,size);
  287.     string[size]=0;
  288.     printf("h=%s\n",string);
  289.     printf("result=%ld\n",result);
  290.     PatchMunger(timeout-LMGetTicks());
  291.     return result;
  292. }